<?php
require_once("DBConfig.php");
class Database{
	private $dbconn = NULL;

	public function Connect($dbc){
		$this->dbconn = new mysqli($dbc->m_host, $dbc->m_user, $dbc->m_pass, $dbc->m_db);
	}

	public function Login($username, $password){
			//Select alla från 'users'
			$loginQuery = "SELECT * FROM users WHERE username = ? AND password = ?";
			//förbereder queryn genom att anropa connection-stringen till databasen
			$stmt = $this->dbconn->prepare($loginQuery);
			//binder username/password
			$stmt->bind_param('ss', $username, $password);
			//exekverar sats
			$stmt->execute();
			//sparar resultat i databas
			$stmt->store_result();
			//num_rows returnerar antal rader från databasen
			$rows = $stmt->num_rows;
			//stänger anslutningen
			$stmt->close();	
			//om det finns mer än 0 rader i databasen returneras true
			if($rows>0){
				return true;
			}else{
				return false;
			}
		}
	
	
	public function Members($username){
			//Select alla från 'users'
			$membQuery = "SELECT * FROM users WHERE username = ? ";
			//förbereder queryn genom att anropa connection-stringen till databasen
			$stmt = $this->dbconn->prepare($membQuery);
			$stmt->bind_param('s', $username);
			
			//exekverar sats
			$membArray = array();
	
			$stmt->close();	
			}
		
	public function IfExists($r_username){

		$sqlExists = "SELECT userID FROM users WHERE username = ?";
		
			$stmt = $this->dbconn->prepare($sqlExists);
		//om inte sql-satsen kan initieras kastas undantag
			if($stmt === FALSE){
				throw new Exception($this->dbconn->error);
			}
			//binder paramteret
			$stmt->bind_param('s', $r_username);
			//kan den inte exekveras kastas undantag
			if (!$stmt->execute())
				throw new Exception();
			//binder resultat
			$stmt->bind_result($result);
			//hämtar resultat
			$stmt->fetch();
			
			if ($result) {
				return true;
			}
			return false;
	}
	//Lägger till medlem genom att inserta in i mysql-databasen
	public function Register($r_username, $r_password){
		$sqlAdd = "INSERT INTO users(username, password) VALUES(?, ?)";

		$stmt = $this->dbconn->prepare($sqlAdd);
		$stmt->bind_param('ss', $r_username, $r_password);

	
			$stmt->execute();
			$stmt->store_result();
			$rows = $stmt->num_rows;
			$stmt->close();	
			
			if($rows>0){
				return true;
			}else{
				return false;
			}
	}
	
	public function AddPost($topic, $text){
		$sqlAddPost = "INSERT INTO posts(topic, text) VALUES(?, ?)";

		$stmt = $this->dbconn->prepare($sqlAddPost);
		$stmt->bind_param('ss', $topic, $text);

	
			$stmt->execute();
			$stmt->store_result();
			$rows = $stmt->num_rows;
			$stmt->close();	
			
			if($rows>0){
				return true;
			}else{
				return false;
			}
	}
	
	//Tar bort medlem genom att deleta från mysql-databasen
	public function Delete($d_username){
		$sqlDel = "DELETE FROM users WHERE username = ?";

		$stmt = $this->dbconn->prepare($sqlDel);

		$stmt->bind_param('s', $d_username);

		$stmt->execute();
	}
	
	//Tar bort medlem genom att deleta från mysql-databasen
	public function DeletePost($id){
		$sqlDelPost = "DELETE FROM posts WHERE postID = ?";

		$stmt = $this->dbconn->prepare($sqlDelPost);

		$stmt->bind_param('i', $id);

		$stmt->execute();
	}
	
	//stänger databas
	public function Close()
	{
		return $this->dbconn->close();
	}
	
	public function CheckPosts(){
			//Select alla från 'users'
			$postQuery = "SELECT postID, topic, text FROM posts ORDER BY postID DESC";
			//förbereder queryn genom att anropa connection-stringen till databasen
			$stmt = $this->dbconn->prepare($postQuery);
			
			$stmt->execute();
			$stmt->bind_result($postID, $topic, $text);
               
			$postArray = new PostArray();
			//en while-loop som om man lyckas hämta bindat result från db görs post om till ett objekt och trycks sedan in i en array
			while ($stmt->fetch())
			{
				$post = new Post($postID, $topic, $text);
				$postArray->add($post);
			}
			//stänger anslutningen
			$stmt->close();	
			
			return $postArray;
			}
	}
	//Skapar arrayen
	class PostArray {
        private $m_postArray = array();
        
        public function __construct() {
        }
        public function add(Post $post) {
        	array_push($this->m_postArray, $post);
        }
        public function get() {
                return $this->m_postArray;
        }
	}
	
	//Skapar objektet
	class Post {
      
	  	private $m_postID; //String
        private $m_topic; //String
        private $m_text; //String
     
        public function __construct($postID, $topic, $text) {
               $this->m_postID = $postID; 
                $this->m_topic = $topic;
                $this->m_text = $text;
		}
        public function getpostID() {
                return $this->m_postID;
        }
        public function gettopic() {
                return $this->m_topic;
        }
		public function gettxt(){
                return $this->m_text; 
        }

	}
?>